﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace GenerateFilesLibrary
{
    public class GetCountryName
    {
        public string GetIPCountryDetails(string ip)
        {
           string rootDirectory = AppDomain.CurrentDomain.BaseDirectory + "IpData";
            string rootFilePath = rootDirectory+"\\IPCountry.txt";
            if (File.Exists(rootFilePath))
            {
                System.IO.StreamReader myFile = new System.IO.StreamReader(rootFilePath);
                string myString = myFile.ReadToEnd();
                myFile.Close();

                string[] ipArr=ip.Split('.');
                string part1 = ipArr[0];
                string part2 = ipArr[1];
                string part3 = ipArr[2];
                string part4 = ipArr[3];

                    string expression = @"\[" + part1 + "\\..*\\]##[A-Z]*@@";
                    MatchCollection col = Regex.Matches(myString, expression);

                    StringBuilder firstMatch = new StringBuilder();
                    foreach (Match m1 in col)
                    {
                        firstMatch.AppendLine(m1.Value);
                    }
                    
                    int secValue=Convert.ToInt32(part2);
                    while (secValue > 0)
                    {
                        string expression2 = @"\[" + part1 + "\\." + secValue.ToString()+ ".*\\]##[A-Z]*@@";
                        MatchCollection col2 = Regex.Matches(firstMatch.ToString(), expression2);
                        if (col2.Count == 0)
                        {
                            secValue = secValue - 1;
                            continue;
                        }
                        else if (col2.Count == 1)
                        {
                            return col2[0].Value;
                        }
                        else//more matches found 2nd item like 120.20.x.x and 120.20.xx.xx
                        {
                            int thirdValue = Convert.ToInt32(part3);
                            while (thirdValue > 0)
                            {
                                string expression3 = @"\[" + part1 + "\\." + secValue.ToString() + "\\." + thirdValue.ToString() + ".*\\]##[A-Z]*@@";
                                MatchCollection col3 = Regex.Matches(firstMatch.ToString(), expression3);
                                if (col3.Count == 0)
                                {
                                    thirdValue = thirdValue - 1;
                                    continue;
                                }
                                else if (col3.Count == 1)
                                {
                                    return col3[0].Value;
                                }
                            }
                        }
                    }

            }
            return "";
        }
        public string GetCountry(string ip)
        {
            if (ip != "")
            {
               string ipCountryData= GetIPCountryDetails(ip);
                String country = Regex.Match(ipCountryData, @"[A-Z]+").Value;
                return country;
            }
            return "";
        }
    }
}
